package com.link_with_health.service.impl;

import com.link_with_health.exception.BaseException;
import com.link_with_health.exception.ModifyDatabaseFailedException;
import com.link_with_health.exception.NullQueryResultException;
import com.link_with_health.mapper.AdminLogMapper;
import com.link_with_health.model.AdminLogEntity;
import com.link_with_health.service.AdminLogService;
import com.link_with_health.service.BaseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.List;

@Service("adminLogService")
public class AdminLogServiceImpl extends BaseService implements AdminLogService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    AdminLogMapper adminLogMapper;

    /**
     * 通过日志id查找操作日志信息
     * @param id 日志id
     * @return 日志信息
     * @throws BaseException 异常
     */
    @Override
    public AdminLogEntity getAdminLogById(Long id) throws BaseException {
        AdminLogEntity entity = null;
        try {
            entity = adminLogMapper.queryAdminLogById(id);
        } catch (Exception e){
            logger.error("[query adminLog failed]",e);
        }
        if (entity == null)
            throw new NullQueryResultException("ADMINLOG.ID: " + id);

        return adminLogMapper.queryAdminLogById(id);
    }

    /**
     * 通过日志类型进行查找日志信息
     * @param type 类型
     * @return 日志信息
     * @throws BaseException 异常
     */
    @Override
    public List<AdminLogEntity> getAdminLogByType(String type) throws BaseException {
        List<AdminLogEntity> logList = null;
        try {
            logList = adminLogMapper.queryAdminLogByType(type);
        } catch (Exception e){
            logger.error("[query adminLog failed]",e);
        }
        if(logList == null || logList.isEmpty())
            throw new NullQueryResultException("ADMINLOG.OPERATION_TYPE: "+type);

        return logList;
    }

    /**
     * 通过操作者id获取日志信息
     * @param operator 操作者id
     * @return 日志信息
     * @throws BaseException 异常
     */
    @Override
    public List<AdminLogEntity> getAdminLogByOperator(Integer operator) throws BaseException{
        List<AdminLogEntity> logList = null;

        try {
            logList = adminLogMapper.queryAdminLogByOperator(operator);
        } catch (Exception e){
            logger.error("[query adminLog failed]",e);
        }

        if (logList == null || logList.isEmpty())
            throw new NullQueryResultException("ADMINLOG.OPERATOR_ID: "+operator);

        return logList;
    }

    /**
     * 获取所有日志信息
     * @return 日志信息
     * @throws BaseException 异常
     */
    @Override
    public List<AdminLogEntity> getAllAdminLog() throws BaseException{
        List<AdminLogEntity> logList = null;

        try {
            logList = adminLogMapper.queryAllAdminLog();
        } catch (Exception e) {
            logger.error("[query all adminLog failed]", e);
        }

        if (logList == null || logList.isEmpty())
            throw new NullQueryResultException("ALL ADMINLOG");

        return logList;
    }

    /**
     * 增加日志信息
     * @param log 日志
     * @return 改动数量
     * @throws BaseException 异常
     */
    @Override
    public int addAdminLog(AdminLogEntity log) throws BaseException{
        int result = 0;
        try {
            result = adminLogMapper.insertAdminLog(log);

        } catch (Exception e) {
            logger.error("[add adminLog failed]",e);
        }
        if(result == 0)
            throw new ModifyDatabaseFailedException("INSERT ADMINLOG FAILED: "+log.toString());
        return result;
    }

    /**
     * 修改日志
     * @param log 日志
     * @return 改动数量
     * @throws BaseException 异常
     */
    @Override
    public int changeAdminLog(AdminLogEntity log) throws BaseException{
        int result = 0;
        try {
            result = adminLogMapper.updateAdminLog(log);
        } catch (Exception e) {
            logger.error("[change adminLog failed]",e);
        }
        if (result == 0)
            throw new ModifyDatabaseFailedException("UPDATE ADMINLOG FAILED: "+log.toString());

        return result;
    }

    /**
     * 删除日志
     * @param id 日志id
     * @return 删除日志
     * @throws BaseException 异常
     */
    @Override
    public int deleteAdminLog(Long id) throws BaseException{
        int result = 0;
        try {
            result = adminLogMapper.deleteAdminLog(id);
        } catch (Exception e){
            logger.error("[delete adminLog failed]",e);
        }

        if(result == 0)
            throw new ModifyDatabaseFailedException("DELETE ADMINLOG FAILED: "+id);
        return result;
    }
}
